go

exec dbo.p_drop_proc_if_exists 'meta_add_small_int_column';

go

--Function of adding new small integer column
create procedure meta_add_small_int_column(
    --Table id
    @a_table_id binary(16),
    --Column id
    @a_column_id binary(16),
    --Column name
    @a_column_name varchar(max))
with execute as owner
as
begin
	set nocount on;
	declare
	--Table name
    @a_table_name varchar(26),
    --Result
    @a_result numeric(1);
    
    exec dbo.p_check_col_not_exists @a_table_id, @a_column_id, @a_column_name, @a_result output;
    
    if @a_result = 0 return;
    
    set xact_abort on;
    begin tran;

    set @a_table_name = dbo.p_get_table_name(@a_table_id);
    
    --Changing the metadata
    insert into meta_small_int_columns(column_id, table_id, column_name,
        order_index, customization_level)
    values (@a_column_id, @a_table_id, @a_column_name,
        dbo.p_generate_order_index(@a_table_id), 1);
        
    --Dropping table view
    exec dbo.p_drop_view_if_exists @a_table_name;
         
    exec ('alter table tbl_' 
    + @a_table_name
    + ' add ' + @a_column_name
    + ' numeric(5)');
         
    --Creating table view
    exec dbo.p_create_table_view @a_table_id;
    
	commit;
end;

go

exec dbo.p_drop_proc_if_exists 'meta_set_small_int_default';

go

--Function of setting column default value
create procedure meta_set_small_int_default(
    --Column id
    @a_column_id binary(16),
    --Default value
    @a_default_value numeric(5))
with execute as owner
as
begin
	set nocount on;
	declare
	--Result
	@a_result numeric(1);
	
    exec dbo.p_check_col_type @a_column_id, 'meta_small_int_columns', 'small int', @a_result output;
    
    if @a_result = 0 return;
    
    set xact_abort on;
    begin tran;
    
    exec dbo.p_set_numeric_default @a_column_id, @a_default_value;
    
    commit;
end;

go

exec dbo.p_drop_proc_if_exists 'meta_set_small_int_maximum';

go

--Function of setting small int column maximum
create procedure meta_set_small_int_maximum(
    --Column id
    @a_column_id binary(16),
    --Maximum
    @a_maximum numeric(5))
with execute as owner
as
begin
	set nocount on;
	declare
	--Result
	@a_result numeric(1),
	--Table id
    @a_table_id binary(16),
    --Table name
    @a_table_name varchar(26),
    --Column name
    @a_column_name varchar(26),
    --Old maximum
    @a_old_maximum numeric(5),
    --Column type
    @a_column_type varchar(30),
    --Column id str
    @a_column_id_str varchar(32);
	
    exec dbo.p_check_col_type @a_column_id, 'meta_small_int_columns', 'small int', @a_result output;
    
    if @a_result = 0 return;
    
    set xact_abort on;
    begin tran;
    
    set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
    
    --Reading column properties
    select @a_old_maximum = maximum,
    @a_column_name = column_name,
    @a_table_id = table_id
    from meta_small_int_columns
    where column_id = @a_column_id;
            
    set @a_table_name = dbo.p_get_table_name(@a_table_id);
    
    --Updating the metadata
    if @a_maximum is not null
        exec ('update meta_small_int_columns'
        + ' set maximum = ' + @a_maximum
        + ' where column_id = 0x' + @a_column_id_str);
    else
        exec ('update meta_small_int_columns'
        + ' set maximum = null'
        + ' where column_id = 0x' + @a_column_id_str);
         
    --There is no need to drop table view
         
    if @a_old_maximum is not null
        --Drop old constraint 
        exec dbo.p_drop_small_int_maximum_check @a_column_id;
         
    --Create new check
    if @a_maximum is not null
        exec dbo.p_create_small_int_maximum_check @a_column_id;
    
    commit;
end;

go

exec dbo.p_drop_proc_if_exists 'p_create_small_int_maximum_check';

go

--Function of creating small int maximum check
create procedure p_create_small_int_maximum_check(
	--Column id
	@a_column_id binary(16))
as
begin
	declare
	--Table name
	@a_table_name varchar(26),
	--Maximum
	@a_maximum numeric(5),
	--Column name
	@a_column_name varchar(26),
	--Column id str
	@a_column_id_str varchar(32);

	set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
	
	--Reading column properties
	select @a_maximum = maximum,
    @a_column_name = column_name
    from meta_small_int_columns
    where column_id = @a_column_id;
    
    select @a_table_name = table_name
    from meta_table_columns
    where column_id = @a_column_id;
    
	exec ('alter table tbl_' 
	+ @a_table_name
	+ ' add constraint x' + @a_column_id_str
	+ ' check(' + @a_column_name
	+ ' <= ' + @a_maximum + ')');
end;

go

exec dbo.p_drop_proc_if_exists 'p_drop_small_int_maximum_check';

go

--Function of creating small int maximum check
create procedure p_drop_small_int_maximum_check(
	--Column id
	@a_column_id binary(16))
as
begin
	declare
	--Table name
	@a_table_name varchar(26),
	--Column id str
	@a_column_id_str varchar(32);

	set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
	
	--Reading column properties
    select @a_table_name = table_name
    from meta_table_columns
    where column_id = @a_column_id;
    
	exec ('alter table tbl_' 
	+ @a_table_name
	+ ' drop constraint x' + @a_column_id_str);
end;

go

exec dbo.p_drop_proc_if_exists 'meta_set_small_int_minimum';

go

--Function of setting small int column minimum
create procedure meta_set_small_int_minimum(
    --Column id
    @a_column_id binary(16),
    --Minimum
    @a_minimum numeric(5))
with execute as owner
as
begin
	set nocount on;
    declare
	--Result
	@a_result numeric(1),
	--Table id
    @a_table_id binary(16),
    --Table name
    @a_table_name varchar(26),
    --Column name
    @a_column_name varchar(26),
    --Old minimum
    @a_old_minimum numeric(5),
    --Column id str
    @a_column_id_str varchar(32);
	
    exec dbo.p_check_col_type @a_column_id, 'meta_small_int_columns', 'small int', @a_result output;
    
    if @a_result = 0 return;
    
    set xact_abort on;
    begin tran;
    
    set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
    
    --Reading column properties
    select @a_table_id = table_id,
    @a_column_name = column_name,
    @a_old_minimum = minimum
    from meta_small_int_columns
    where column_id = @a_column_id;
    
    set @a_table_name = dbo.p_get_table_name(@a_table_id);
    
    --Updating the metadata
    if @a_minimum is not null
        exec ('update meta_small_int_columns'
        + ' set minimum = ' + @a_minimum
        + ' where column_id = 0x' + @a_column_id_str);
    else
        exec ('update meta_small_int_columns'
        + ' set minimum = null'
		+ ' where column_id = 0x' + @a_column_id_str);
     
    --There is no need to drop table view
     
	if @a_old_minimum is not null
		--Drop old constraint 
		exec dbo.p_drop_small_int_minimum_check @a_column_id;

	--Create new check
	if @a_minimum is not null
		exec dbo.p_create_small_int_minimum_check @a_column_id;
    
    commit;
end;

go

exec dbo.p_drop_proc_if_exists 'p_create_small_int_minimum_check';

go

--Function of creating small int minimum check
create procedure p_create_small_int_minimum_check(
	--Column id
	@a_column_id binary(16))
as
begin
	declare
	--Table name
	@a_table_name varchar(26),
	--Minimum
	@a_minimum numeric(5),
	--Column name
	@a_column_name varchar(26),
	--Column id str
	@a_column_id_str varchar(32);

	set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
	
	--Reading column properties
	select @a_minimum = minimum,
    @a_column_name = column_name
    from meta_small_int_columns
    where column_id = @a_column_id;
    
    select @a_table_name = table_name
    from meta_table_columns
    where column_id = @a_column_id;
    
	exec ('alter table tbl_' 
	+ @a_table_name
	+ ' add constraint n' + @a_column_id_str
	+ ' check(' + @a_column_name
	+ ' >= ' + @a_minimum + ')');
end;

go

exec dbo.p_drop_proc_if_exists 'p_drop_small_int_minimum_check';

go

--Function of creating small int minimum check
create procedure p_drop_small_int_minimum_check(
	--Column id
	@a_column_id binary(16))
as
begin
	declare
	--Table name
	@a_table_name varchar(26),
	--Column id str
	@a_column_id_str varchar(32);

	set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
	
	--Reading column properties
    select @a_table_name = table_name
    from meta_table_columns
    where column_id = @a_column_id;
    
	exec ('alter table tbl_' 
	+ @a_table_name
	+ ' drop constraint n' + @a_column_id_str);
end;

go

exec dbo.p_drop_proc_if_exists 'p_create_small_int_col_constrs';

go

--Function of creating all small int column constraints
create procedure p_create_small_int_col_constrs(
	--Column id
	@a_column_id binary(16))
as
begin
	declare
	--Minimum
	@a_minimum numeric(5),
	--Maximum
	@a_maximum numeric(5);
	
	select @a_minimum = minimum,
	@a_maximum = maximum
	from meta_small_int_columns
	where column_id = @a_column_id;
	
	if @a_minimum is not null
		exec dbo.p_create_small_int_minimum_check @a_column_id;
	if @a_maximum is not null
		exec dbo.p_create_small_int_maximum_check @a_column_id;
end;

go

exec dbo.p_drop_proc_if_exists 'p_drop_small_int_col_constrs';

go

--Function of dropping all small int column constraints
create procedure p_drop_small_int_col_constrs(
	--Column id
	@a_column_id binary(16))
as
begin
	declare
	--Minimum
	@a_minimum numeric(5),
	--Maximum
	@a_maximum numeric(5);
	
	select @a_minimum = minimum,
	@a_maximum = maximum
	from meta_small_int_columns
	where column_id = @a_column_id;
	
	if @a_minimum is not null
		exec dbo.p_drop_small_int_minimum_check @a_column_id;
		
	if @a_maximum is not null
		exec dbo.p_drop_small_int_maximum_check @a_column_id;
end;